﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ForkCarRelationManager.DB;
using ForkCarRelationManager.db;
using System.IO;

namespace ForkCarRelationManager.Controls
{
    public partial class SetControl : UserControl
    {
        int userId;
        dbOperate dbo = new dbOperate();
        public SetControl()
        {
            InitializeComponent();
        }

        private void textBoxX4_TextChanged(object sender, EventArgs e)
        {

        }

        private void SetControl_Load(object sender, EventArgs e)
        {
            userId = LoginInfo.CurrentUser.Account;
            if (userId != -1)
            {
                //加载用户名
                this.TBUserName.Text = LoginInfo.CurrentUser.AccountName;

                //加载提醒设置
                DataTable userTable = dbo.GetDate("select * from Admin where ID = " + userId);
                if (userTable.Rows[0] != null)
                {
                    this.IntputMaintain.Text = userTable.Rows[0]["MaintainTime"].ToString();
                    this.IntputMainten.Text = userTable.Rows[0]["MaintenTime"].ToString();
                }
            }
         
        }

        //修改密码（用户名不让修改）
        private void btnUpdatePassword_Click(object sender, EventArgs e)
        {
            if (userId!=-1)
            {
                if (this.TBPassword.Text.Trim() == "")
                {
                    MessageBox.Show("请输入原始密码！");
                }
                else if (this.TBNewPassword.Text.Trim() == "")
                {
                    MessageBox.Show("请输入新密码！");
                }
                else if (this.TBConfirmPassword.Text.Trim() == "")
                {
                    MessageBox.Show("请确认密码！");
                }
                else { 
                    DataTable userTable = dbo.GetDate("select * from Admin where ID = " + userId);
                    if (userTable.Rows[0]["PassWord"].ToString().CompareTo(this.TBPassword.Text) == 0)
                    {
                        if (this.TBNewPassword.Text.Trim() == this.TBConfirmPassword.Text.Trim())
                        {
                            if (this.TBNewPassword.Text.Trim() == this.TBPassword.Text.Trim())
                            {
                                //满足条件，保存数据
                                String sqlStr = "UPDATE Admin SET Admin.[PassWord] = '" + this.TBConfirmPassword.Text.Trim() + "' where ID = " + userId;
                                bool mark = dbo.UpdateAndInsert(sqlStr);
                                if (mark)
                                {
                                    MessageBox.Show("密码修改成功,请重新登录！");
                                    LoginInfo.CurrentUser.Account = -1;
                                    LoginInfo.CurrentUser.AccountName = "";
                                    this.ParentForm.Close();
                                    FormLogin login = new FormLogin();
                                    login.Show();
                                }
                            }
                            else
                            {
                                MessageBox.Show("新密码不能与原密码相同！");
                            }
                           
                        }
                        else {
                            MessageBox.Show("请重新确认密码！");
                        }
                    }
                    else
                    {
                        MessageBox.Show("原密码输入错误！");
                    }
                }
            }
        }

        //设置提醒时间
        private void buttonX2_Click(object sender, EventArgs e)
        {
            if (IntputMaintain.Text.Trim() != ""&&IntputMainten.Text.Trim()!="")
            {
                String sqlStr = "UPDATE Admin SET Admin.[MaintainTime] = " + Convert.ToInt32(this.IntputMaintain.Text.Trim()) + ",Admin.[MaintenTime] = " + Convert.ToInt32(this.IntputMainten.Text.Trim()) + " where ID = " + userId;
                bool mark = dbo.UpdateAndInsert(sqlStr);
                if (mark)
                {
                    MessageBox.Show("设置成功！");
                }else
                    MessageBox.Show("设置失败！");
            }
        }

        private void buttonX3_Click(object sender, EventArgs e)
        {
            string dataBasePath = Application.StartupPath + "\\db\\forkDatabase.mdb";
            string backPath;
            if (!File.Exists(dataBasePath))
            {
                throw new Exception("源数据库不存在，无法备份");
            }
            else
            {
                FolderBrowserDialog dilog = new FolderBrowserDialog();
                dilog.Description = "请选择文件保存路径";
                if (dilog.ShowDialog() == DialogResult.OK || dilog.ShowDialog() == DialogResult.Yes)
                {
                    backPath = dilog.SelectedPath;
                    File.Copy(dataBasePath, backPath + "\\"+"forkDatabase.mdb", true);
                    MessageBox.Show("数据库备份成功");
                }
            
            }
            
        }

        //一键还原数据库
        private void buttonX4_Click(object sender, EventArgs e)
        {
            String backFilePath;
            string dataBasePath = Application.StartupPath + "\\db\\forkDatabase.mdb";
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);
            openFileDialog.Filter = "数据库文件(*.mdb)|*.mdb|所有文件(*.*)|*.*";
            if (openFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                backFilePath = openFileDialog.FileName;
                if (!File.Exists(backFilePath))
                {
                    throw new Exception("备份数据库不存在，无法修复");
                }
                try
                {
                    //为了没有备份当前数据库，所以在还原前，先备份一下现在的数据库
                    File.Copy(dataBasePath, Application.StartupPath + "\\beifen\\" + "forkDatabaseBack.mdb", true);
                    //如果有重名文件 不提示 ，直接替换
                    File.Copy(backFilePath, dataBasePath, true);
                    MessageBox.Show("数据库还原成功");
                }
                catch
                {
                    MessageBox.Show("数据库还原失败");
                } 
            }
        }
    }
}
